//
//  CoreDataHelper.m
//  Bango
//

#import "CoreDataHelper.h"
#import "Profile.h"


@implementation CoreDataHelper

// gets the most recently added profile, or nil
+(Profile *) getMostRecentProfile :(NSManagedObjectContext *)context {
	
	Profile *result = nil;
	NSMutableArray *results = [self getProfilesByDateDesc:context];
	if([results count] > 0) {
		result = [results objectAtIndex:0];
	} else {
		NSLog(@"no profiles found");
	}
	return result;
	
}

// gets all profiles in date order with the most recently added Profile at the front
+(NSMutableArray *) getProfilesByDateDesc :(NSManagedObjectContext *)context {
	// sort by date
	NSString *sortByField = @"created";
	NSMutableArray* mutableFetchResults = [self getObjectsFromContext:@"Profile" :sortByField :NO :context];
	return mutableFetchResults;
}

// gets all profiles in date order with the most recently added Profile at the front
+(NSMutableArray *) getEventsForProfile :(Profile*)profile context:(NSManagedObjectContext *)context {
	// sort by date
	NSString *sortByField = @"eventTime";
	
	// Create a query predicate to find all child objects of the profile
	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(profile == %@)", profile];
	NSMutableArray* mutableFetchResults = [self searchObjectsInContext:@"Event" : predicate : sortByField :NO :context];
	
	return mutableFetchResults;
}

// gets all profiles in date order with the oldest Profile at the front
+(NSMutableArray *) getProfilesByDateAsc :(NSManagedObjectContext *)context {
	// sort by date
	NSString *sortByField = @"created";
	NSMutableArray* mutableFetchResults = [self getObjectsFromContext:@"Profile" :sortByField :YES :context];
	return mutableFetchResults;
}

+(NSMutableArray *) searchObjectsInContext: (NSString*)entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL) sortAscending : (NSManagedObjectContext*) context {
	
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
	[request setEntity:entity];	
	
	// If a predicate was passed, pass it to the query
	if(predicate != nil)
	{
		[request setPredicate:predicate];
	}
	
	// If a sort key was passed, use it for sorting.
	if(sortKey != nil)
	{
		NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
		NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
		[request setSortDescriptors:sortDescriptors];
		[sortDescriptors release];
		[sortDescriptor release];
	}
	
	NSError *error;
	
	NSMutableArray *mutableFetchResults = [[[context executeFetchRequest:request error:&error] mutableCopy] autorelease];
	
	[request release];
	
	return mutableFetchResults;
}


+(NSMutableArray *) getObjectsFromContext: (NSString*) entityName : (NSString*) sortKey : (BOOL) sortAscending : (NSManagedObjectContext *) context
{
	return [self searchObjectsInContext:entityName :nil :sortKey :sortAscending :context];
}



@end
